<template>
  <j-modal :title="title" :width="850" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 3 }" :wrapper-col="{ span: 20 }">
      <a-form-model-item label="检测地点" required prop="adminDivisions">
        <z-linkage-area placeholder="请选择行政区划" :disabled="false" v-model="form.adminDivisions" @rows="onRows"
          @change="setAdminDivisions" />
      </a-form-model-item>
      <a-form-model-item label="详细地址" required prop="detectCityAreaAddress">
        <a-input v-model="form.detectCityAreaAddress" />
      </a-form-model-item>
      <a-form-model-item label="对接人" required prop="dockingId">
        <a-select v-model="form.dockingId" placeholder="请选择">
          <a-select-option @click.native="onClickItem(item)" v-for="item in dockingArr" :key="item.id"
            :value="item.id">{{ item.personnelName }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="联系电话" required prop="dockingPhone">
        <a-input v-model="form.dockingPhone" placeholder="请输入" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { queryDepartTreeList } from '@/api/api'
import Vue from 'vue'
import { getDivisionAdmin } from "@api/sjsk/specialist";
import * as adressApi from '@/api/sjsk/check/adress.js'
import { USER_INFO } from "@/store/mutation-types"
import { getAction } from '@/api/manage'
export default {
  name: 'CancelModal',
  props: ['title'],
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        adminDivisions: [{ required: true, message: '请选择' }],
        detectCityAreaAddress: [{ required: true, message: '请输入' }],
        dockingId: [{ required: true, message: '请选择' }],
        dockingPhone: [{ required: true, message: '请输入' }],
      },
      isAdd: true,
      loading: false,
      departmentId: '',
      dockingArr: [] //注意value是字符串
    }
  },
  async created() {

  },
  methods: {
    show(record) {
      this.visible = true
      this.form = {}
      this.getUserDeparts();
      if (record == undefined) {
        this.isAdd = true
      } else {
        this.isAdd = false
        this.form = record
        this.form.dockingId = record.dockingId.toString()
      }
    },
    handleCancel() {
      this.visible = false
    },
    // 获取行政区划的值
    setAdminDivisions(value) {
      // console.log("value")
      // console.log(value)
      // let data = value? value.filter(item=>{ return item }): []
      // let lastData = data.length>0?data[data.length-1]:''
      // this.$set(this.model, 'adminDivision', lastData)
      // this.$set(this.model, 'adminDivisionLevel', data.length)
      // this.$set(this.model, 'adminDivisionStr', data.toString())
    },
    onRows(row) {
      console.log("value")
      console.log(row)
    },
    //根据用户id获取部门id,部门取最后一级部门
    getUserDeparts() {
      let that = this;
      let userInfo = Vue.ls.get(USER_INFO)
      getAction('/sys/user/userDepartList', { userId: userInfo.id }).then((res) => {
        if (res.success) {
          let len = res.result.length
          that.form.departmentId = res.result[len - 1].key //部门id
          this.loadDepart()
        }
      })
    },
    //根据部门id获取所在的组织架构
    loadDepart() {
      queryDepartTreeList({ ids: this.form.departmentId, type: 1 }).then(res => {
        if (res.success) {
          this.form.provinceCityAreaCode = res.result[0].adminDivisionStr //保存行政区划编码
          let areaArr = res.result[0].adminDivisionStr.split(',')
          this.$set(this.form, "adminDivisions", areaArr)
          this.getAreaTittle(areaArr)
          this.getDivisionAdmin(this.form.provinceCityAreaCode)
        }
      })
    },
    //根据行政区划编码获取行政区划名称
    getAreaTittle(areaArr) {
      adressApi.getArea({ codeList: areaArr }).then(res => {
        if (res.success) {
          let str = ""
          res.result.forEach(item => {
            str += item.name + "," //保存行政区划名称
          })
          this.form.provinceCityAreaName = str.slice(0, str.length - 1) //删除最后一个逗号
        }
      })
    },
    getDivisionAdmin(departmentId) {
      let pageFilter = {
        pageNo: 1,
        pageSize: 1000,
        division: departmentId
      }
      getDivisionAdmin(pageFilter).then(ser => {
        if (ser.success) {
          this.dockingArr = ser.result.records
          // console.log("1111111111")
          // console.log(this.dockingArr)
          // this.result = ser.result.administratorsDto
          // console.log(ser)
          // console.log(6)
        }
      }).catch(err => {

      })
    },
    onChange(e) {
      console.log(e)
    },
    onClickItem(item) {
      console.log(item)
      this.form.dockingName = item.personnelName
    },
    handleOk() {
      let api
      if (this.isAdd) {
        api = adressApi.addTestNoticeRecord(this.form)
      } else {
        api = adressApi.updateTestNoticeRecord(this.form)
      }
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>

</style>
